﻿
#ifndef _MAP_PLAYER_MGR_H_
#define _MAP_PLAYER_MGR_H_

#include <unordered_map>
#include "../share/common_singleton.h"
#include "../share/jsonmessage.h"
#include "Player.h"
#include "../share/proxy_head.h"
#include "../share/object.h"
#include "../share/timer.h"


typedef std::unordered_map< int, OBJ_ID_TYPE > TObjIDMap;
typedef std::unordered_map< int, int > TPlayerIDMapIDMap;
typedef std::vector< int > TPlayerDestroyVector;

class CMapPlayerManger :public common::CSingleton<CMapPlayerManger>
{
public:
	CMapPlayerManger(){}
	~CMapPlayerManger(){}
public:
	void Initialize();
	Player* GetPlayerByPlayerID(int playerId);
	Player* GetPlayerByObjID(OBJ_ID_TYPE obj_id);
	Player* GetPlayerBySocket(int socket);
	int32_t GetMapIdByPlayerId(int32_t playerId);
	int32_t GetOnlineCount() { return player_id_map_.size(); }
	void TickUpdate();

	void OnPlayerDisconnect(int32_t fd);
	void CloseTcpConnect(int32_t fd);
	int PlayerLeaveGame(Player& player);
	int PlayerEnterGame(Player& player);
	void AddPlayerDestroy(Player& player);
	int RegistePlayerSocket(int32_t fd_, int obj_id);
	int UnRegistePlayerSocket(int32_t fd_);

	void SetPlayerId2MidMap(uint32_t playerId, uint32_t mapid);

	void PlayerDestroyDoing();
	// game同步到map的玩家数据
	void UpdateLoginPlayerInfo(std::string& playerInfo);
	std::string GetLoginPlayerInfo(uint32_t playerId);
	// 更新玩家数量到webredis
	void UpdatePlayerCountToWebRedis();

	void OnPlayerEnterScene(int32_t playerid);
	Player* GetPlayerByClientKey(uint64_t client_key);

private:
	TObjIDMap player_id_map_;
	TObjIDMap socket_id_map_;
	TPlayerIDMapIDMap m_pid2mapid;
	TPlayerDestroyVector player_destroy_;

private:
	std::unordered_map<uint32_t, std::string> m_pid2playerInfo;
	uint32_t m_last_playerCount;	// 上次存入redis的人数
	uint64_t m_last_updateCount_time;//上次存入redis的时间
};

#endif